<!--
 * @Descripttion: 项目根组件
 * @Author: congz
 * @Date: 2020-06-04 11:22:40
 * @LastEditors: congz
 * @LastEditTime: 2020-09-01 14:34:14
-->

<template>
  <div id='app' name='app'>
    <el-container>
      <!-- 顶部导航栏 -->

      <div class='topbar' v-show='$route.meta.showMenu!==false'>
        <div class='nav'>
          <ul>
            <li>
              <div style='margin-top:5px;font-size:16px'>
                <router-link to='/goods'>全部商品</router-link>
              </div>
            </li>
            <li v-if='!this.$store.getters.getUser'>
              <div style='margin-top:5px;font-size:16px'>
                <router-link to='/login'>登录</router-link>
              </div>
            </li>
            <li v-else class='header-user-con'>
              <!-- 用户头像 -->
              <div class='user-avator'>
                <img :src='`${baseURL()}/media/avatar/${this.$store.getters.getUser.id}.jpg`' />
              </div>
              <!-- 用户名下拉菜单 -->
              <div class='user-name'>
                <el-dropdown>
                  <span class='el-dropdown-link'>
                    {{ this.$store.getters.getUser.nickname }}
                    <i class='el-icon-caret-bottom'></i>
                  </span>
                  <el-dropdown-menu slot='dropdown'>
                    <a href='https://github.com/congz666/cmall-go' target='_blank'>
                      <el-dropdown-item>项目仓库</el-dropdown-item>
                    </a>
                    <router-link to='/center'>
                      <el-dropdown-item>个人中心</el-dropdown-item>
                    </router-link>
                    <a @click='logout'>
                      <el-dropdown-item>退出登录</el-dropdown-item>
                    </a>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </li>
            <li class='font'>
              <router-link to='/favorite'>我的收藏</router-link>
            </li>
            <li class='font'>
              <router-link to='/user/address'>我的地址</router-link>
            </li>
            <li class='font'>
              <router-link to='/order'>我的订单</router-link>
            </li>
            <li :class="getNum > 0 ? 'shopCart-full' : 'shopCart'">
              <div style='margin-top:5px'>
                <router-link to='/cart'>
                  <i class='el-icon-shopping-cart-full'></i> 购物车
                  <span>({{ getNum }})</span>
                </router-link>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <!-- 顶部导航栏END -->

      <!-- 顶栏容器 -->

      <el-header v-show='$route.meta.showMenu!==false' style='height:80px'>
        <el-row type='flex' justify='start' align='middle'>
          <el-col :span='3'>
            <div class='logo'>
              <router-link to='/'>
                <img src='./assets/imgs/beilin.png' alt>
              </router-link>
            </div>
          </el-col>
          <el-col :span='15'>
            <el-input placeholder='请输入搜索内容' v-model='search' @input='searchClick'>
              <el-select v-model='select' slot='prepend' placeholder='宝贝'>
                <el-option label='天猫' value='1'></el-option>
                <el-option label='店铺' value='2'></el-option>
              </el-select>
              <el-button slot='append' icon='el-icon-search' @click='searchClick' @input='searchClick'
                         type='warning'></el-button>
              <el-button slot='append' icon='el-icon-camera' @click='uploading' type='warning'></el-button>
            </el-input>
            <el-link href='https://element.eleme.io' target='_blank' :underline='false'>新款连衣裙</el-link>
            <el-link href='https://element.eleme.io' target='_blank' :underline='false'>四件套</el-link>
            <el-link href='https://element.eleme.io' target='_blank' :underline='false'>潮流T恤</el-link>
            <el-link href='https://element.eleme.io' target='_blank' :underline='false'>时尚女鞋</el-link>
            <el-link href='https://element.eleme.io' target='_blank' :underline='false'>短裤</el-link>
            <el-link href='https://element.eleme.io' target='_blank' :underline='false'>半身裙</el-link>
          </el-col>
        </el-row>
      </el-header>
      <el-divider />
      <el-dialog :visible.sync='upload'>
        <el-upload
          class='upload-demo'
          drag
          fullscreen
          :action='`${baseURL()}/pictureSearch/`'
          multiple
          :on-success='handleAvatarSuccess'
          ref="my-upload"
          style='text-align: center'
        >
          <i class='el-icon-upload'></i>
          <div class='el-upload__text'>将文件拖到此处，或<em>点击上传</em></div>
          <div class='el-upload__tip' slot='tip'>只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-dialog>
      <!-- 顶栏容器END -->

      <!-- 主要区域容器 -->
      <el-main>
        <keep-alive>
          <router-view :key='key'></router-view>
        </keep-alive>
      </el-main>
      <!-- 主要区域容器END -->

      <!-- 底栏容器 -->
      <el-footer>
        <div class='footer'>
          <div class='ng-promise-box'>
            <div class='ng-promise'>
              <p class='text'>
                <a class='icon1' href='javascript:;'>7天无理由退换货</a>
                <a class='icon2' href='javascript:;'>满99元全场免邮</a>
                <a class='icon3' style='margin-right: 0' href='javascript:;'>100%品质保证</a>
              </p>
            </div>
          </div>
          <div class='github'>
            <a href='https://github.com/congz666/cmall-go' target='_blank'>
              <div class='github-but'></div>
            </a>
          </div>
          <div class='mod_help'>
            <p>
              <router-link to='/'>首页</router-link>
              <span>|</span>
              <router-link to='/goods'>全部商品</router-link>
              <span>|</span>
              <router-link to='/about'>关于我们</router-link>
            </p>
            <p>
              Copyright 北京林业大学信息学院
              <iframe
                style='margin-left: 2px; margin-bottom:-5px;'
                frameborder='0'
                scrolling='0'
                width='91px'
                height='20px'
                src='https://ghbtns.com/github-btn.html?user=congz666&repo=cmall-go&type=star&count=true'
              ></iframe>
            </p>
            <a href='http://www.beian.miit.gov.cn/' target='_blank'>粤ICP备20067893号</a>
          </div>
        </div>
      </el-footer>
      <!-- 底栏容器END -->
    </el-container>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { mapGetters } from 'vuex'
import * as cartsAPI from '@/api/carts'
import { baseURL } from '@/api/utils/request'

export default {
  beforeUpdate() {
    this.activeIndex = this.$route.path
  },
  data() {
    return {
      activeIndex: '', // 头部导航栏选中的标签
      search: '', // 搜索条件
      upload: false
    }
  },
  beforeCreate() {
    document.querySelector('body').setAttribute('style', 'background:#f5f5f5')
  },
  created() {
    this.setUser(JSON.parse(localStorage.getItem('user')))
    this.search = ''
  },
  computed: {
    ...mapGetters(['getUser', 'getNum']),
    key() {
      return this.$route.path + Math.random()
    }
  },
  watch: {
    // 获取vuex的登录状态
    getUser: function(val) {
      console.log(val)
      if (val === '') {
        // 用户没有登录
        this.setShoppingCart([])
      } else {
        // 用户已经登录,获取该用户的购物车信息
        cartsAPI
          .showCarts(val.id)
          .then(res => {
            if (res.status === 200) {
              if (res.data === null) {
                this.setShoppingCart([])
              } else {
                //给res
                this.setShoppingCart(res.data.items)
                console.log(res.data.items)
              }
            } else if (res.status === 20001) {
              //token过期，需要重新登录
              this.loginExpired(res.msg)
            } else {
              this.$notify.error({
                title: '购物车获取失败',
                message: res.msg
              })
            }
          })
          .catch(err => {
            return Promise.reject(err)
          })
      }
    },

    // 添加观察者来观察路由变化
    $route(to) {
      // 如果返回到当前页面，重置 search 的值
      console.log(to.path)
      if (to.path === '/') {
        this.search = ''
      }
    }
  },
  methods: {
    ...mapActions(['setUser', 'setShoppingCart']),
    login() {
      this.$router.push({
        name: 'Login'
      })
    },
    handleAvatarSuccess(res) {
      if(res.result !== ''){
        this.upload = false
        this.$router.push({ path: '/goods', query: { search: res.result } })
        this.$refs['my-upload'].clearFiles();
      }

    },
    uploading() {
      this.upload = true
    },
    baseURL() {
      return baseURL
    },
    // 退出登录
    logout() {
      // 清空本地登录信息
      localStorage.removeItem('user')
      localStorage.removeItem('token')
      // 清空vuex登录信息
      this.setUser('')
      this.$router.push({
        name: 'Home'
      })
      this.notifySucceed('登出成功')
    },
    //重定向
    register() {
      this.$router.push({
        name: 'Register'
      })
    },
    // 点击搜索按钮
    searchClick() {
      if (this.search !== '') {
        // 跳转到全部商品页面,并传递搜索条件
        this.$router.push({ path: '/goods', query: { search: this.search } })
        // this.search = ''
      }
    }
  }
}
</script>

<style>
/* 全局CSS */
* {
  padding: 0;
  margin: 0;
  border: 0;
  list-style: none;
}

.header-user-con {
  display: flex;
  height: 50px;
  align-items: center;
}

.el-input {
  margin-top: 8px;
}

.el-link {
  margin-top: 10px;
  margin-right: 30px;
  margin-left: 5px;
}


.user-name {
  margin-left: 10px;
}

.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}


.logo img {
  margin-top: 5px;
  width: auto; /* 根据实际情况调整 */
  height: 80px; /* 根据实际情况调整 */
}

.el-row {
  margin: 0 auto;
  width: 80%; /* 根据实际情况调整 */
}

.el-dropdown-menu__item {
  text-align: center;
}

.el-dropdown-item:hover {
  color: #ff6700;
}

#app .el-header {
  padding: 0;
}

#app .el-main {
  min-height: 300px;
  padding: 0;
}

#app .el-footer {
  padding: 0;
}

a,
a:hover {
  text-decoration: none;
}

/* 全局CSS END */

/* 顶部导航栏CSS */
.topbar {
  height: 50px;
  background-color: #303643;
}

.topbar .nav {
  width: 1225px;
  margin: 0 auto;
}

.topbar .nav ul {
  float: right;
}

.topbar .nav li {
  float: left;
  height: 50px;
  color: #b0b0b0;
  font-size: 16px;
  text-align: center;
  line-height: 40px;
  margin-left: 20px;
}

.topbar .nav li.font {
  margin-top: 5px;
}

.topbar .nav li .router-link {
  margin-top: 5px;
}


.topbar .nav li a {
  color: #b0b0b0;
}

.topbar .nav a:hover {
  color: #fff;
}

.topbar .nav .shopCart {
  width: 120px;
  background: #424242;
}

.topbar .nav .shopCart:hover {
  background: #fff;
}

.topbar .nav .shopCart:hover a {
  color: #ff6700;
}

.topbar .nav .shopCart-full {
  width: 120px;
  background: #ff6700;
}

.topbar .nav .shopCart-full a {
  color: white;
}

/* 顶部导航栏CSS END */

/* 顶栏容器CSS */
.el-header {
  margin-top: 5px;
  margin-left: 300px;
  height: 100px;
  padding: 0;
}

/* 顶栏容器CSS END */

/* 底栏容器CSS */
.footer {
  width: 100%;
  text-align: center;
  background: #2f2f2f;
  padding-bottom: 20px;
}

.footer .ng-promise-box {
  border-bottom: 1px solid #3d3d3d;
  line-height: 145px;
}

.footer .ng-promise-box {
  margin: 0 auto;
  border-bottom: 1px solid #3d3d3d;
  line-height: 145px;
}

.footer .ng-promise-box .ng-promise p a {
  color: #fff;
  font-size: 20px;
  margin-right: 210px;
  padding-left: 44px;
  height: 40px;
  display: inline-block;
  line-height: 40px;
  text-decoration: none;
  background: url('./assets/imgs/us-icon.png') no-repeat left 0;
}

.footer .github {
  height: 50px;
  line-height: 50px;
  margin-top: 20px;
}

.footer .github .github-but {
  width: 50px;
  height: 50px;
  margin: 0 auto;
  background: url('./assets/imgs/github.png') no-repeat;
}

.footer .mod_help {
  text-align: center;
  color: #888888;
}

.footer .mod_help p {
  margin: 20px 0 16px 0;
}

.footer .mod_help p a {
  color: #888888;
  text-decoration: none;
}

.footer .mod_help p a:hover {
  color: #fff;
}

.footer .mod_help p span {
  padding: 0 22px;
}

.footer .mod_help a {
  color: #888888;
}

/* 底栏容器CSS END */
</style>
